// 使用 云片发送短信验证码

var https = require('https');
var qs = require('querystring');

var apikey = '22eccd2e29dc6f8370c2d08b02f30d02';
// 修改为您要发送的手机号码，多个号码用逗号隔开
var mobile = '13698352476';
// 指定发送的模板编号
var tpl_id = 3433986;
// 指定发送模板的内容
var tpl_value =  {'#code#':'123456'};
// 智能匹配模板发送https地址
var sms_host = 'sms.yunpian.com';
// 指定模板发送接口https地址
send_tpl_sms_uri = '/v2/sms/tpl_single_send.json';




send_tpl_sms(send_tpl_sms_uri,apikey,mobile,tpl_id,tpl_value);


function send_tpl_sms(uri,apikey,mobile,tpl_id,tpl_value){
    var post_data = {  
    'apikey': apikey,
    'mobile':mobile,
    'tpl_id':tpl_id,
    'tpl_value':qs.stringify(tpl_value),  
    };//这是需要提交的数据  
    var content = qs.stringify(post_data);  
    post(uri,content,sms_host); 
}
function post(uri,content,host){
    var options = {  
        hostname: host,
        port: 443,  
        path: uri,  
        method: 'POST',  
        headers: {  
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'  
        }  
    };
    var req = https.request(options, function (res) {  
        // console.log('STATUS: ' + res.statusCode);  
        // console.log('HEADERS: ' + JSON.stringify(res.headers));  
        res.setEncoding('utf8');  
        res.on('data', function (chunk) {  
            console.log('BODY: ' + chunk);  
        });  
    }); 
    //console.log(content);
    req.write(content);  

    req.end();   
}
